﻿Imports utilitiesLibrary

Public Class pizzaFunctions

    Dim ht As Hashtable
    Dim objDB As DBConnect
    Dim hasht As New Hashtable

    'Passing the Session object to the class constructor
    Public Sub New(ByVal db As DBConnect)
        objDB = db

        'initialize hash table
        hasht.Add("Small 1", "$7.25")
        hasht.Add("Small 2", "$7.50")
        hasht.Add("Small 3", "$7.75")
        hasht.Add("Small 4", "$8.00")
        hasht.Add("Medium 1", "$8.25")
        hasht.Add("Medium 2", "$8.50")
        hasht.Add("Medium 3", "$8.75")
        hasht.Add("Medium 4", "$9.00")
        hasht.Add("Large 1", "$9.25")
        hasht.Add("Large 2", "$9.50")
        hasht.Add("Large 3", "$9.75")
        hasht.Add("Large 4", "$10.00")
    End Sub


    'calculates the price of pizza based on size and price class
    Public Function pizzaPrice(ByVal size As String, ByVal PType As String) As Decimal
        Dim pClass As Integer
        Dim piePrice As Decimal
        Dim htKey As String

        'do DB work
        Dim sql As String = "SELECT PriceClass FROM PIZZA WHERE PizzaType='" & PType & "'"
        'MsgBox(sql)
        objDB.getDataSet(sql)
        pClass = objDB.getField("PriceClass", 0)
        htKey = size & " " & pClass
        'MsgBox("HT key: " & htKey)

        'compute individual pie price
        piePrice = hasht(htKey)
        'MsgBox("test:" & piePrice)

        Return piePrice
    End Function

    'checks if the pizza ordered has a coupon associated with it
    Public Function coupon(ByVal pizzaType As String) As String
        Dim couponSql As String = "SELECT coupon FROM PIZZA WHERE PizzaType ='" & pizzaType & "'"
        Dim couponExists As String

        objDB.getDataSet(couponSql)
        couponExists = objDB.getField("Coupon", 0)

        Return couponExists
    End Function

    Public Sub addPieToSales(ByVal pizzaType As String, ByVal cost As Decimal)
        'adds price of individual pies to total sales
        Dim currentSales As Decimal
        Dim newSales As Decimal
        Dim getTotalSql As String = "SELECT TotalSales FRom PIZZA WHERE PizzaType='" & pizzaType & "'"
        Dim insertSql As String


        objDB.getDataSet(getTotalSql)
        currentSales = objDB.getField("TotalSales", 0)
        newSales = currentSales + cost
        insertSql = "UPDATE PIZZA SET TotalSales=" & newSales & " " & "WHERE PizzaType='" & pizzaType & "'"
        objDB.doUpdate(insertSql)


    End Sub

    Public Function getTotalSales(pizzaType As String) As Decimal
        Dim sql As String = "SELECT TotalSales FROM PIZZA WHERE PizzaType='" & pizzaType & "'"
        Dim sales As Decimal

        objDB.getDataSet(sql)
        sales = objDB.getField("TotalSales", 0)

        Return sales

    End Function


End Class

'properties of each line item ordered 
Public Class orderLine
    Dim pzzaType As String
    Dim size As String
    Dim qty As Integer
    Dim price As Decimal
    Dim totalCost As Decimal
    Dim coupon As String

    Public Property pizzaType() As String
        Get
            Return pzzaType
        End Get
        Set(ByVal value As String)
            pzzaType = value
        End Set
    End Property

    Public Property pieSize() As String
        Get
            Return size
        End Get
        Set(ByVal value As String)
            size = value
        End Set
    End Property

    Public Property quantity() As Integer
        Get
            Return qty
        End Get
        Set(ByVal value As Integer)
            qty = value
        End Set
    End Property

    Public Property piePrice() As Decimal
        Get
            Return price
        End Get
        Set(ByVal value As Decimal)
            price = value
        End Set
    End Property

    Public Property total() As Decimal
        Get
            Return totalCost
        End Get
        Set(ByVal value As Decimal)
            totalCost = value
        End Set
    End Property

    Public Property isCoupon() As String
        Get
            Return coupon
        End Get
        Set(ByVal value As String)
            coupon = value
        End Set
    End Property
End Class